import subprocess
import threading

def checkHost(ip):
    ping_cmd = "ping -n 1 -w 1 %s" % ip
    null_file = open("win_null", mode="wb")
    # subprocess.call(cmd, shell=True) 适用于获取命令的执行状态码
    ping_result = subprocess.call(ping_cmd, shell=True, stdout=null_file, stderr=null_file)
    if ping_result == 0:
        print("Host %s is up" % ip)
    null_file.close()
# 检测10.11.4.0/24网段所有在线主机, 多线程  threading.Thread(), 创建线程
def checkSubnet():
    for i in range(1, 255):
        ip = "10.11.4.%s" % i
        # 创建线程, target=函数名称，执行的操作；  args=， 用于给函数传参, 数据格式必须元组
        ping_thread = threading.Thread(target=checkHost, args=(ip,))
        ping_thread.start()
        # checkHost(ip=ip)

if __name__ == '__main__':
    checkSubnet()